In this example we look at how to synchronize the motion of a shared
object in various clients, so that it moves at the same time in each client.
There are two important aspects to this :
- The master should be used to synchronize motion since local computer speeds
may vary and we cannot trust local clocks to keep in synch.
- New clients must obtain the current state (position, orientation, color,
anything else which may vary). This means that on starting up, they must
query the master on the current state and receive back this state as a
parameter in the RPC call. In example 1 this was not done, so that a new
client does not pick up the current orientation of the box - it only keeps
track of future rotations.
In the following example we illustrate these concepts by sharing a ball
which moves in a loop. Any new client picks up the current position of the
ball and then keeps in sync. Notice also that though there are local clocks
we only trigger motion via the master's clock to the master and then to an RPC
call.
The basic logic is as follows :
- The timeSensor send a time event to move(). This is ignored by
everyone except the master. The master computes the new position of the
ball and encodes the coordinates in a string s. Then it makes an RPC call
to everyone else passing s as the parameter. Finally it moves the ball to
the new position in its own local copy using the method place_ball.
- When a client world receives the RPC call from the master, it enters
RPC_place_ball. This function decodes the parameter string s to obtain the
new position and moves the ball to this new position using its local copy of
place_ball.
This example consists of:
- The source code
- An active example of this world (must have CP installed as your
default VRML browser), and
- Exercises for you to try on your own.